import { createStore } from 'vuex'
import user from '@/store/login/login'
import { system } from '@/store/main/system/system'
import { getUserListRequest } from '@/service/mian/system/system'
import analysis from '@/store/main/analysis/analysis'
export const store = createStore({
  state: {
    departmentList: [],
    roleList: [],
    menuList: []
  },
  getters: {},
  mutations: {
    changeDepartmentState(state, list) {
      state.departmentList = list
    },
    changeRoleState(state, list) {
      state.roleList = list
    },
    changeMenuState(state, list) {
      state.menuList = list
    }
  },
  actions: {
    async getRoleAndDepartData({ commit }) {
      const departmentRes = await getUserListRequest('/department/list', {
        offset: 0,
        size: 100
      })
      const { list: departmnetList } = departmentRes.data
      commit('changeDepartmentState', departmnetList)
      const roleRes = await getUserListRequest('/role/list', {
        offset: 0,
        size: 100
      })
      const { list: roleList } = roleRes.data
      commit('changeRoleState', roleList)
      //请求菜单列表
      const menuRes = await getUserListRequest('/menu/list', {})
      const { list: menuList } = menuRes.data
      commit('changeMenuState', menuList)
    }
  },
  modules: { user, system, analysis }
})
export const setupStore = () => {
  store.dispatch('getRoleAndDepartData')
  store.dispatch('user/loadState')
}
